package meijuli.com.join51.business.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import meijuli.com.join51.business.Confirm;
import meijuli.com.join51.business.R;
import meijuli.com.join51.business.activity.CertificationActivity;
import meijuli.com.join51.business.activity.MainActivity;
import meijuli.com.join51.business.adapter.ConfirmAdapter;

import com.jhx.common.cache.SimpleCache;
import com.jhx.common.framework.QuickBaseFragment;
import com.jhx.common.http.ApiManager;
import com.jhx.common.http.HttpRequest;
import com.jhx.common.http.HttpResponse;
import com.jhx.common.http.ResponseCallback;
import com.jhx.common.model.User;

import meijuli.com.join51.business.model.PlatformDiscount;
import meijuli.com.join51.business.service.ApiService;
import com.jhx.common.utils.UserManager;
import retrofit2.Call;

/**
 * 确认页面
 *
 * @author wenqin 2017-04-14 17:08
 */

public class ConfirmFragment extends QuickBaseFragment {
    private List<Confirm> mConfirmList;
    private ConfirmAdapter mAdapter;
    private Call<HttpResponse<String>> mCall;
    private User userInfo;
    RecyclerView mConfirmRv;
    TextView mPreviousBtn;
    TextView mNextBtn;

    @Override
    public View bindView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_confirm, container, false);
        mConfirmRv = view.findViewById(R.id.confirm_rv);
        mPreviousBtn = view.findViewById(R.id.previous_btn);
        mNextBtn = view.findViewById(R.id.next_btn);
        mPreviousBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onPreviousBtnClick();
            }
        });
        mNextBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                requestUserInfo();
            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (UserManager.get().checkUserNull(getActivity())) {
            return;
        }
        userInfo = UserManager.get().getUser();
        initView();
        initData(userInfo.getType());
    }

    private void initView() {
        mConfirmList = new ArrayList<>();
        mAdapter = new ConfirmAdapter(getActivity(), mConfirmList);
        mConfirmRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        mConfirmRv.setNestedScrollingEnabled(false);
        mConfirmRv.setAdapter(mAdapter);

        mNextBtn.setText("确定");
        mPreviousBtn.setText("修改");
    }

    void onPreviousBtnClick() {
        getActivity().onBackPressed();
    }

    void requestUserInfo() {
        showLoadingDialog();
        Long discountId = SimpleCache.Companion.get(PlatformDiscount.discontId);
        Map<String, String> map = new HashMap<>();
        map.put("registNum", userInfo.getRegisterNum());
        map.put("shopPhoto", userInfo.getShopPhoto());
        map.put("longitude", userInfo.getLongitude() + "");
        map.put("latitude", userInfo.getLatitude() + "");
        map.put("license", userInfo.getLicense());
        map.put("shopAdd", userInfo.getShopAdd());
        map.put("address", userInfo.getAddress());
        map.put("provinceId", userInfo.getProvinceId() + "");
        map.put("cityId", userInfo.getCityId() + "");
        map.put("townId", userInfo.getTownId() + "");
        map.put("businessName", userInfo.getBusinessName());
        map.put("phone", userInfo.getPhone());
        map.put("dmId", userInfo.getDmId() + "");
        map.put("IDCardNum", userInfo.getIDCard());
        map.put("IDback", userInfo.getLegalIDback());
        map.put("IDfront", userInfo.getLegalIDfront());
        map.put("legal", userInfo.getLegal());
        map.put("oneCategoryId", userInfo.getOneCategoryId());
        map.put("discountId", discountId + "");
        mCall = ApiManager.get().getApiService(ApiService.class).pushUserInfo(map);
        HttpRequest.getRequest().autoRecycle(this).start(mCall, new ResponseCallback() {
            @Override
            public void onSuccess(String url, Object data) {
                hideLoadingDialog();
                Intent intent = new Intent(getActivity(), MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }

            @Override
            public void onError(String url, int errorCode, String errorMsg) {
                super.onError(url, errorCode, errorMsg);
                hideLoadingDialog();
            }
        });
    }

    private void initData(int type) {
        mConfirmList.clear();
        addConfirmContent(userInfo.getBusinessName(), "商户名称：");
        addConfirmContent(userInfo.getRegisterNum(), "注册号：");
        addConfirmContent(userInfo.getLocalLicense(), "营业执照图片：", false);
        addConfirmContent(userInfo.getLegal(), "法人姓名：");
        addConfirmContent(userInfo.getIDCard(), "身份证号：");
        addConfirmContent(userInfo.getLocalIDfront() +
                "," + userInfo.getLocalIDback(), "身份证图片：", false);
        addConfirmContent(userInfo.getCategoryName(), "行业分类：");
        addConfirmContent(userInfo.getAddress() + userInfo.getShopAdd(), "商铺地址：");
        addConfirmContent(userInfo.getProvinceName() + " "
                + userInfo.getCityName() + " " + userInfo.getTownName(), "商铺区域：");
        addConfirmContent(userInfo.getLocalShopPhoto(), "商铺图片：", false);
        mAdapter.notifyDataSetChanged();
    }

    private void addConfirmContent(String content, String title, boolean isShowText) {
        Confirm confirm = new Confirm();
        confirm.setContent(content);
        confirm.setShowText(isShowText);
        confirm.setTitle(title);
        mConfirmList.add(confirm);
    }

    private void addConfirmContent(String content, String title) {
        addConfirmContent(content, title, true);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        ((CertificationActivity)getActivity()).setToolbarTitle("商家认证信息");
    }
}
